package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @GetMapping("/list")
    public PageDTO<Item> getitem(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                  @RequestParam(value = "size",defaultValue = "5") Integer size){
//        PageInfo<Item> item = itemService.getitem(page,size);
        Page<Item> result = itemService.page(new Page<>(page,size));
//        IPage<Item> iPage = new Page
        return new PageDTO<>(result.getTotal(),result.getRecords());
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getByid(@PathVariable Long id){
        return itemService.getById(id);
    }

    /**
     * 新增
     * @param item
     */
    @PostMapping
    public void add(@RequestBody Item item){
        itemService.save(item);
    }

    /**
     *
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void up(@PathVariable Long id,@PathVariable Integer status){
        Item byId = itemService.getById(id);
        if(status == 1){
            byId.setStatus(1);
            itemService.updateById(byId);
        }else if(status == 2){
            byId.setStatus(2);
            itemService.updateById(byId);
        }
    }

    /**
     * 根据id修改商品
     * @param item
     */
    @PutMapping
    public void update(@RequestBody Item item){
        itemService.updateById(item);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id){
        itemService.removeById(id);
    }

}
